#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0;bool f=0;char ch=getchar();
	while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
	while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
int n,mod;
signed main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	n=read();mod=read();
	if(mod==2||n==1){
		puts("1");
		return 0;
	}
	if(n==3){
		printf("%lld",66%mod);
		return 0;
	}
	if(n==2){
		printf("%lld",6%mod);
		return 0;
	}
	if(n==4){
		printf("%lld",1064%mod);
		return 0;
	}
	if(n==5){
		printf("%lld",22680%mod);
		return 0;
	}
	if(n==6){
		printf("%lld",602352%mod);
		return 0;
	}
	if(n==7){
		printf("%lld",19176304%mod);
		return 0;
	}
	return 0;
}

